package com.sharemarking.wa.platform.service;

import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.cms.CmsChannelListDto;
import com.sharemarking.wa.platform.dto.cms.CmsPostInfoDto;
import com.sharemarking.wa.platform.dto.cms.CmsPostListDto;
import com.sharemarking.wa.platform.mapper.CmsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class CmsService {
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    CmsMapper cmsMapper;

    public ResponseParams<?> getMostNesChannelList() {

        List<CmsChannelListDto> cmsListDto = cmsMapper.getMostNesChannelList();

        return ResponseParams.ok(cmsListDto);
    }


    public ResponseParams<?> getCmsPostList() throws Exception {
        String channelId = request.getParameter("channelId");

        if (StringUtils.isEmpty(channelId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("channelId"));
        }

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>(16);
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("channelId", channelId);

        List<CmsPostListDto> list = cmsMapper.getCmsPostList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, cmsMapper.getCmsPostTotal(params), list.size() == pageSize));

    }

    public ResponseParams<?> getCmsPostInfo() throws Exception {
        String articleId = request.getParameter("articleId");

        if (StringUtils.isEmpty(articleId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("articleId"));
        }

        CmsPostInfoDto cmsPostInfoDto = cmsMapper.getCmsPostInfo(Integer.valueOf(articleId));


        return ResponseParams.ok(cmsPostInfoDto);
    }

    public void getArticle() throws Exception {
        String articleId = request.getParameter("id");

        if (StringUtils.isEmpty(articleId)) {
            throw RequestException.create(ResponseParams.requiredParamsIsMissing("id"));
        }

        CmsPostInfoDto cmsPostInfoDto = cmsMapper.getCmsPostInfo(Integer.valueOf(articleId));

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<html><head><title>" + cmsPostInfoDto.getTitle() + "</title></head><body>" + cmsPostInfoDto.getContent() + "</body></html>");
        out.close();
    }
}
